using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using SharpCore.Data;
using SharpCore.Extensions;
using SharpCore.Utilities;

namespace MMS.DAO
{
	public class UnitDAO
	{
		#region Fields

		private string connectionStringName;
       
		#endregion

		#region Constructors

		public UnitDAO(string connectionStringName)
		{
			ValidationUtility.ValidateArgument("connectionStringName", connectionStringName);

			this.connectionStringName = connectionStringName;
		}

		#endregion

		#region Methods

		/// <summary>
		/// Saves a record to the Unit table.
		/// </summary>
		public void Insert(UnitDTO unit)
		{
			ValidationUtility.ValidateArgument("unit", unit);

			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@Id", unit.Id),
				new SqlParameter("@UnitName", unit.UnitName),
				new SqlParameter("@Note", unit.Note)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "sp_UnitInsert", parameters);
		}

		/// <summary>
		/// Updates a record in the Unit table.
		/// </summary>
		public void Update(UnitDTO unit)
		{
			ValidationUtility.ValidateArgument("unit", unit);

			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@Id", unit.Id),
				new SqlParameter("@UnitName", unit.UnitName),
				new SqlParameter("@Note", unit.Note)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "sp_UnitUpdate", parameters);
		}

		/// <summary>
		/// Deletes a record from the sp_Unit table by its primary key.
		/// </summary>
		public void Delete(string id)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@Id", id)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "sp_UnitDelete", parameters);
		}

		/// <summary>
		/// Selects a single record from the Unit table.
		/// </summary>
		public UnitDTO Select(string id)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@Id", id)
			};

			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "sp_UnitSelect", parameters))
			{
				if (dataReader.Read())
				{
					return MapDataReader(dataReader);
				}
				else
				{
					return null;
				}
			}
		}

		/// <summary>
		/// Selects a single record from the Unit table.
		/// </summary>
		public string SelectJson(string id)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@Id", id)
			};

			return SqlClientUtility.ExecuteJson(connectionStringName, CommandType.StoredProcedure, "sp_UnitSelect", parameters);
		}

		/// <summary>
		/// Selects all records from the Unit table.
		/// </summary>
		public List<UnitDTO> SelectAll()
		{
			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "sp_UnitSelectAll"))
			{
				List<UnitDTO> unitDTOList = new List<UnitDTO>();
				while (dataReader.Read())
				{
					UnitDTO unitDTO = MapDataReader(dataReader);
					unitDTOList.Add(unitDTO);
				}

				return unitDTOList;
			}
		}

		/// <summary>
		/// Selects all records from the Unit table.
		/// </summary>
		public string SelectAllJson()
		{
			return SqlClientUtility.ExecuteJson(connectionStringName, CommandType.StoredProcedure, "sp_UnitSelectAll");
		}

		/// <summary>
		/// Creates a new instance of the UnitDTO class and populates it with data from the specified SqlDataReader.
		/// </summary>
		private UnitDTO MapDataReader(SqlDataReader dataReader)
		{
			UnitDTO unitDTO = new UnitDTO();
			unitDTO.Id = dataReader.GetString("Id", String.Empty);
			unitDTO.UnitName = dataReader.GetString("UnitName", null);
			unitDTO.Note = dataReader.GetString("Note", null);

			return unitDTO;
		}

       
		#endregion

        public UnitDTO selectFromName(string unitName)
        {
            SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@UnitName", unitName)
			};

            using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "sp_UnitSelectFromName", parameters))
            {
                if (dataReader.Read())
                {
                    return MapDataReader(dataReader);
                }
                else
                {
                    return null;
                }
            }
        }
        public bool isIdUsing(string id)
        {
             MaterialDAO materialDAO = new MaterialDAO(Param.ConnectionString.Value);
            bool result = false;
            MaterialDTO materialDTO = materialDAO.selectByIdUnit(id);
            if (null != materialDTO)
            {
                result = true;
            }
            return result;
        }
    }
}
